// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var WebModel = require("WebModel");
var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: 
    {
      createRoomBoard : {
              default: null,
              type: cc.Prefab
      },

      joinRoomBoard : {
        default: null,
        type: cc.Prefab
      },

      settingBoard : {
        default: null,
        type: cc.Prefab
      },

      maskLayer : {
        default: null,
        type: cc.Prefab
      },

    	nStage : {
            	default : 0,
            	visible : false
        },

      m_createRoomBoard : {
              default : null,
              visible : false
      },

      m_joinRoomBoard : {
              default : null,
              visible : false
      },

      m_maskLayer : {
              default : null,
              visible : false
      },

      m_bTryContinueGame : {
              default : false,
              visible : false
      },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function () 
    {
        // this.node.on('mousedown', function (event) 
        // {
        //     cc.log("startLayer mousedown");
        // }, this);

        this.node.on('event_start_game', function (event)
        {
            var s = cc.director.loadScene("GameLayer");
        }, this);

        this.node.on('event_continue_game', function (event)
        {
            var s = cc.director.loadScene("GameLayer");
        }, this);

        this.node.on('event_create_room_success', function (event)
        {
            var nRoomID = event.detail.nRoomID;

            if (this.m_createRoomBoard != null)
            {
                var createRoomScript = this.m_createRoomBoard.getComponent("CreateRoomScript");
                createRoomScript.setRoomNum(nRoomID);
            }

            if (this.m_maskLayer != null)
            {
              var maskLayerScript = this.m_maskLayer.getComponent('MaskScript');
              maskLayerScript.showWaitingLabel("等待玩家加入...");
            }

        }, this);

        this.node.on('event_join_room_success', function (event)
        {
            if (this.m_joinRoomBoard != null)
            {
                var joinRoomScript = this.m_joinRoomBoard.getComponent("JoinRoomScript");
                joinRoomScript.onJoinRoomOK();
            }

            if (this.m_maskLayer != null)
            {
              var maskLayerScript = this.m_maskLayer.getComponent('MaskScript');
              maskLayerScript.showWaitingLabel("等待玩家加入...");

              var rgPlayerNames = event.detail.rgPlayerNames;
              var strPlayerNames = "";

              for (var i = 0; i < rgPlayerNames.length; i++)
              {
                  strPlayerNames += rgPlayerNames[i] + " 已准备\r\n"
              }

              maskLayerScript.addShowLabel(strPlayerNames);
            }

        }, this);

        this.node.on('event_web_opened', function (event)
      	{
      		this.startLayerStage0to1();
          if (this.m_maskLayer != null)
          {
            this.m_maskLayer.removeFromParent();
            this.m_maskLayer = null;
          }
      	}, this);

        this.node.on('event_error_no_room', function (event)
        {
          cc.log('event_error_no_room');

          if (this.m_joinRoomBoard != null)
          {
              var joinRoomScript = this.m_joinRoomBoard.getComponent("JoinRoomScript");
              joinRoomScript.showNoRoomError();

              this.m_maskLayer.removeFromParent();
              this.m_maskLayer = null;
          }
          else if (this.m_bTryContinueGame == true)
          {
              this.m_bTryContinueGame = false;

              Take6Model.savePlayerIndex(-1);
              Take6Model.saveRoomID(0);

              this.m_maskLayer.removeFromParent();
              this.m_maskLayer = null;

              var returnRoomButton = this.node.getChildByName('ReturnRoomButton');
              returnRoomButton.active = false;
          }

        }, this);

        this.node.on('event_sending_message_to_server', function (event)
        {
          var maskLayer = cc.instantiate(this.maskLayer);
          this.m_maskLayer = maskLayer;

          var maskLayerScript = maskLayer.getComponent('MaskScript');
          maskLayerScript.showWaitingLabel();

          this.node.addChild(maskLayer, 999);
        }, this);

        // this.node.on('event_web_onmessage', function (event)
        // {
        //   console.log("get event_web_onmessage event");
        //   if (this.m_maskLayer != null)
        //   {
        //     console.log("this.m_maskLayer");
            
        //     this.m_maskLayer.removeFromParent();
        //     this.m_maskLayer = null;
        //   }
        // }, this);

        this.node.on('event_web_onerror', function (event)
        {
          if (this.m_maskLayer != null)
          {
            var maskLayerScript = this.m_maskLayer.getComponent('MaskScript');
            maskLayerScript.showWaitingLabel("服务器错误...");
          }
        }, this);

        this.node.on('event_web_onclose', function (event)
        {
          if (this.m_maskLayer != null)
          {
            var maskLayerScript = this.m_maskLayer.getComponent('MaskScript');
            maskLayerScript.showWaitingLabel("服务器关闭...");
          }
        }, this);

        this.node.on('event_game_off_line', function (event)
        {
           cc.director.loadScene("StartLayer");
        }, this);

        var playerNameLabelNode = this.node.getChildByName("ShowPlayerNameLabel");
        playerNameLabelNode.getComponent(cc.Label).string = Take6Model.getPlayerName();

        cc.log('m_bIsConnected : ', WebModel.m_bIsConnected);

        if (Take6Model.getPlayerName() == "" || Take6Model.getPlayerName() == null )
        {
            this.onSettingButtonPressed();
        }
        else if (WebModel.m_bIsConnected == true)
        {
            var connectButton = this.node.getChildByName('ConnectButton');
            connectButton.active = false;

            var joinRoomButton = this.node.getChildByName('JoinRoomButton');
            joinRoomButton.active = true;
            var playWithFriendButton = this.node.getChildByName('PlayWithFriendButton');
            playWithFriendButton.active = true;
        }
    },

    onConnectButtonPressed : function ()
    {
        WebModel.connectToServer()
    },

    onJoinRoomButtonPressed : function ()
    {
        Take6Model.playerJoinRoom(-1);
    },

    onReturnRoomButtonPressed : function ()
    {
        this.m_bTryContinueGame = true;
        Take6Model.playerReturnRoom();    
    },

    onJoinRoomWithRoomIDButtonPressed : function ()
    {
        var board = cc.instantiate(this.joinRoomBoard);
        this.node.addChild(board, 100);

        this.m_joinRoomBoard = board;
    },

    onCreateRoomButtonPressed : function ()
    {
    	  var board = cc.instantiate(this.createRoomBoard);
        this.node.addChild(board, 100);

        this.m_createRoomBoard = board;
    },

    onSettingButtonPressed : function ()
    {
        var board = cc.instantiate(this.settingBoard);
        this.node.addChild(board, 100);
    },

    setPlayerName : function ()
    {
        var editBox = this.node.getChildByName('PlayerNameEditBox');
        var playerName = editBox.getComponent(cc.EditBox).string;
        Take6Model.setPlayerName(playerName);

        var playerNameLabelNode = this.node.getChildByName("ShowPlayerNameLabel");
        playerNameLabelNode.getComponent(cc.Label).string = Take6Model.getPlayerName();
    },

    // 从开始游戏 到 快速开始与好友开黑
   	startLayerStage0to1 : function ()
   	{
   		var connectButton = this.node.getChildByName('ConnectButton');
   		connectButton.active = false;

   		var joinRoomButton = this.node.getChildByName('JoinRoomButton');
   		joinRoomButton.active = true;
   		var playWithFriendButton = this.node.getChildByName('PlayWithFriendButton');
   		playWithFriendButton.active = true;
      var returnRoomButton = this.node.getChildByName('ReturnRoomButton');
      returnRoomButton.active = Take6Model.getRoomID() > 0 && Take6Model.getPlayerIndex() > -1;

   		var startSprite = this.node.getChildByName('StartSprite');
   		startSprite.runAction(cc.moveBy(1.0, cc.p(0, 200)));

   		var titleLabel = this.node.getChildByName('TitleLabel');
   		titleLabel.runAction(cc.fadeOut(1.0));
   		var versionLabel = this.node.getChildByName('VersionLabel');
   		versionLabel.runAction(cc.fadeOut(1.0));

      cc.log('m_bIsConnected : ', WebModel.m_bIsConnected);
   	},

   	// 从快速开始与好友开黑 到 建房与加房
   	startLayerStage1to2 : function ()
   	{
        var joinRoomButton = this.node.getChildByName('JoinRoomButton');
        joinRoomButton.active = false;
        var playWithFriendButton = this.node.getChildByName('PlayWithFriendButton');
        playWithFriendButton.active = false;
        var returnRoomButton = this.node.getChildByName('ReturnRoomButton');
        returnRoomButton.active = false;

        var createRoomButton = this.node.getChildByName('CreateRoomButton');
        createRoomButton.active = true;
        var joinFriendRoomButton = this.node.getChildByName('JoinFriendRoomButton');
        joinFriendRoomButton.active = true;

        var backtoState1Button = this.node.getChildByName('backToStage1Button');
        backtoState1Button.active = true;
   	},

   	startLayerStage2to1 : function ()
   	{
        var joinRoomButton = this.node.getChildByName('JoinRoomButton');
        joinRoomButton.active = true;
        var playWithFriendButton = this.node.getChildByName('PlayWithFriendButton');
        playWithFriendButton.active = true;

        var createRoomButton = this.node.getChildByName('CreateRoomButton');
        createRoomButton.active = false;
        var joinFriendRoomButton = this.node.getChildByName('JoinFriendRoomButton');
        joinFriendRoomButton.active = false;

        var backtoState1Button = this.node.getChildByName('backToStage1Button');
        backtoState1Button.active = false;
   	},

    //test
    onGameLayerButtonPressed : function ()
    {
        cc.director.loadScene("GameLayer");
    },

    // start () {

    // },

    // update (dt) {},
});
